<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0,user-scalable=no,minimal-ui">
    <link rel="icon" href="../img/asset-favico.ico">
    <title>编辑信息</title>
    <!--引入样式-->
    <link rel="stylesheet" href="../element-ui/index.css"/>
    <link rel="stylesheet" href="../css/page-health-orderInfo.css"/>
</head>
<body data-spy="scroll" data-target="#myNavbar" data-offset="150">
<div id="app" class="app">
    <!-- 页面头部 -->
    <div class="top-header">
        <span class="f-left"><i class="icon-back" onclick="history.go(-1)"></i></span>
        <span class="center">健康医疗</span>
        <span class="f-right"><i class="icon-more"></i></span>
    </div>
    <!-- 页面内容 -->
    <div class="contentBox">
        <div class="card">
            <div class="">
                <img :src="domain + setmeal.img" v-if="setmeal.img != null && setmeal.img != ''"/>
            </div>
            <div class="project-text">
                <h4 class="tit">{{setmeal.name}}</h4>
                <p class="subtit">{{setmeal.remark}}</p>
                <p class="keywords">
                    <span>{{setmeal.sex == '0' ? '性别不限' : setmeal.sex == '1' ? '男' : '女'}}</span>
                    <span>{{setmeal.age}}</span>
                </p>
            </div>
            <div class="project-know">
                <a href="orderNotice.html" class="link-page">
                    <i class="icon-ask-circle"><span class="path1"></span><span class="path2"></span></i>
                    <span class="word">预约须知</span>
                    <span class="arrow"><i class="icon-rit-arrow"></i></span>
                </a>
            </div>
        </div>
        <div class="form-info">
            <div class="info-title">
                <span class="name">体检人信息</span>
            </div>
            <form class="info-form">
                <div class="input-row">
                    <label>体检人</label>
                    <input v-model="member.name" type="text" class="input-clear" placeholder="请输入姓名">
                </div>
                <div class="input-row single-radio">
                    <label class="radio-title">性别</label>
                    <div class="radio-list">
                        <div class="radio">
                            <input v-model="member.sex" id="item1" type="radio" name="sex" value="1" checked>
                            <label for="item1"></label>
                            <span>男</span>
                        </div>
                        <div class="radio">
                            <input v-model="member.sex" id="item2" type="radio" name="sex" value="2">
                            <label for="item2"></label>
                            <span>女</span>
                        </div>
                    </div>
                </div>
                <div class="input-row">
                    <label>手机号</label>
                    <input style="padding-left: 28px;" v-model="member.phoneNumber" type="text"
                           class="input-clear" placeholder="请输入手机号">
                    <input style="font-size: x-small;" id="validateCodeButton" @click="sendOrderCode()"
                           type="button" value="发送验证码">
                </div>
                <div class="input-row">
                    <label>验证码</label>
                    <input v-model="orderCode" type="text" class="input-clear" placeholder="请输入验证码"
                           @blur="checkOrderCode()">
                </div>
                <div class="input-row">
                    <label>身份证号</label>
                    <input v-model="member.idCard" type="text" class="input-clear" placeholder="请输入身份证号">
                </div>
                <div class="date">
                    <label>体检日期</label>
                    <i class="icon-date" class="picktime"></i>
                    <input v-model="order.orderDate" type="text" class="picktime"
                           style="padding-left: 15px;" readonly placeholder="请选择体检日期">
                </div>
            </form>
            <div class="box-button">
                <button @click="submitOrder()" type="button" class="btn order-btn">提交预约</button>
            </div>
        </div>
    </div>
</div>

<!--引入js-->
<script src="../js/common.js"></script>
<script src="../js/jquery-3.5.1.js"></script>
<script src="../js/datePicker.js"></script>
<script src="../js/axios.min.js"></script>
<script src="../js/vue.js"></script>
<script src="../element-ui/index.js"></script>
<script>
    var vue = new Vue({
        el: "#app",
        data: {
            setmeal: {}, //套餐详情
            domain: "", //图片所在域名
            orderCode: "", //验证码
            member: {}, //会员信息
            order: {}, //预约信息
            orderCode_check: false, //验证码校验
            orderDate_check: false, //体检日期校验
            order_check: false //预约重复校验
        },
        created() {
            //查询套餐基本信息
            this.findById();
        },
        mounted() {
            //初始化日期选择控件
            this.initCalendar();
        },
        methods: {
            //提交预约
            async submitOrder() {
                //打开全屏遮罩
                var fullscreenLoading = this.$loading();

                //校验手机号输入是否正确
                if (!checkTelephone(this.member.phoneNumber)) {
                    this.$message.warning("请输入正确的手机号");
                    fullscreenLoading.close();
                    return false;
                }
                //校验身份证号格式
                if (!checkIdCard(this.member.idCard)) {
                    this.$message.warning("请输入正确的身份证号");
                    fullscreenLoading.close();
                    return false;
                }

                //校验短信验证码
                await this.checkOrderCode();
                if (!this.orderCode_check) {
                    fullscreenLoading.close();
                    return false;
                }
                //校验体检日期
                await this.checkOrderDate();
                if (!this.orderDate_check) {
                    fullscreenLoading.close();
                    return false;
                }

                //获取会员id
                await this.findMemberId();
                //判断是否重复预约
                await this.checkOrder();
                if (!this.order_check) {
                    fullscreenLoading.close();
                    return false;
                }

                //提交预约信息
                axios.post("../order/add", this.order)
                    .then(res => {
                        if (res.data.flag) {
                            this.$message.success(res.data.msg);
                            //跳转到预约成功页面
                            location.href = "orderSuccess.html?orderId=" + res.data.data;
                        } else {
                            this.$message.warning(res.data.msg);
                        }

                        //关闭全屏遮罩
                        fullscreenLoading.close();
                    })
                    .catch(error => {
                        this.$message.error("请求出错：" + error);
                        //关闭全屏遮罩
                        fullscreenLoading.close();
                    });
            },
            //判断是否重复预约
            async checkOrder() {
                await axios.post("../order/checkOrder", this.order)
                    .then(res => {
                        if (res.data.flag) {
                            this.order_check = true;
                        } else {
                            this.order_check = false;
                            this.$message.warning(res.data.msg);
                        }
                    })
                    .catch(error => {
                        this.order_check = false;
                        this.$message.error("请求出错：" + error);
                    });
            },
            //获取会员id
            async findMemberId() {
                await axios.post("../member/findMemberId", this.member)
                    .then(res => {
                        if (res.data.flag) {
                            this.order.memberId = res.data.data;
                        } else {
                            this.$message.warning(res.data.msg);
                        }
                    })
                    .catch(error => {
                        this.$message.error("请求出错：" + error);
                    });
            },
            //校验体检日期
            async checkOrderDate() {
                var orderDate = this.order.orderDate;
                if (orderDate == null || orderDate == "") {
                    this.$message.warning("请选择体检日期");
                    this.orderDate_check = false;
                    return false;
                }

                var url = "../orderSetting/checkOrderDate?orderDate=" + orderDate;
                await axios.get(url)
                    .then(res => {
                        if (res.data.flag) {
                            this.orderDate_check = true;
                        } else {
                            this.orderDate_check = false;
                            this.$message.warning(res.data.msg);
                        }
                    })
                    .catch(error => {
                        this.orderDate_check = false;
                        this.$message.error("请求出错：" + error);
                    });
            },
            //校验短信验证码
            async checkOrderCode() {
                if (this.orderCode === "") {
                    this.orderCode_check = false;
                    this.$message.warning("请填写验证码");
                    return false;
                }

                var url = "../authCode/checkOrderCode/" + this.member.phoneNumber
                    + "/" + this.orderCode;
                await axios.get(url)
                    .then(res => {
                        if (res.data.flag) {
                            this.orderCode_check = true;
                        } else {
                            this.orderCode_check = false;
                            this.$message.warning(res.data.msg);
                        }
                    })
                    .catch(error => {
                        this.orderCode_check = false;
                        this.$message.error("请求出错：" + error);
                    });
            },
            //发送短信验证码
            sendOrderCode() {
                //获取用户输入的手机号
                var phone = this.member.phoneNumber;
                //校验手机号输入是否正确
                if (!checkTelephone(phone)) {
                    this.$message.warning("请输入正确的手机号");
                    return false;
                }
                //发送验证码的按钮button
                validateCodeButton = $("#validateCodeButton")[0];
                //每秒执行一次doLoop
                clock = window.setInterval(doLoop, 1000);

                //请求后端发送验证码
                axios.get("../authCode/sendOrderCode/" + phone)
                    .then(res => {
                        if (res.data.flag) {
                            this.$message.success(res.data.msg);
                        } else {
                            this.$message.warning(res.data.msg);
                        }
                    })
                    .catch(error => {
                        this.$message.error("请求出错：" + error);
                    });
            },
            //查询套餐基本信息
            findById() {
                //打开全屏遮罩
                var fullscreenLoading = this.$loading();
                //获取地址栏参数id
                var id = getUrlParam("id");
                this.order.setmealId = id;

                axios.get("../setMeal/findById/" + id)
                    .then(res => {
                        if (res.data.flag) {
                            //套餐信息
                            this.setmeal = res.data.data.setmeal;
                            //图片所在域名
                            this.domain = res.data.data.domain;
                        } else {
                            this.$message.warning(res.data.msg);
                        }

                        //关闭全屏遮罩
                        fullscreenLoading.close();
                    })
                    .catch(error => {
                        this.$message.error("请求出错：" + error);
                        //关闭全屏遮罩
                        fullscreenLoading.close();
                    });
            },
            //初始化日期选择控件
            initCalendar() {
                //日期控件
                var calendar = new datePicker();
                calendar.init({
                    'trigger': '.picktime', //按钮选择器，用于触发弹出插件
                    'type': 'date', //模式：date年月日
                    'minDate': getSpecifiedDate(new Date(), 1), //最小日期
                    'maxDate': getSpecifiedDate(new Date(), 15) //最大日期
                });
            }
        }
    });
</script>
</body>
</html>